package com.shangcheng.orders.service.impl;


import com.shangcheng.goods.feign.SkuFeign;
import com.shangcheng.goods.feign.SpuFeign;
import com.shangcheng.goods.pojo.Sku;
import com.shangcheng.orders.dao.OrderItemMapper;
import com.shangcheng.orders.pojo.OrderItem;
import com.shangcheng.orders.service.CartService;
import entity.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述
 *
 * @author www.itheima.com
 * @version 1.0
 * @package com.changgou.order.service.impl *
 * @since 1.0
 */
@Service
public class CartServiceImpl implements CartService {

    @Autowired
    private SpuFeign spuFeign;

    @Autowired
    private SkuFeign skuFeign;

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Autowired
    private IdWorker idWorker;

    @Override
    public void add(Long skuId, Integer num, String username) {
        //1.查询redis中相对应的商品信息
        OrderItem orderItem = orderItemMapper.selectById(skuId);
        if (orderItem != null) {
            //2.如果当前商品在redis中的存在,则更新商品的数量与价钱
            orderItem.setNum(num + 1);
            orderItem.setMoney(orderItem.getNum() * orderItem.getPrice());
            orderItem.setPayMoney(orderItem.getNum() * orderItem.getPrice());
            orderItemMapper.updateByPrimaryKey(orderItem);
        } else {
            Sku sku = skuFeign.findById(skuId).getData();
            //封装orderItem
            orderItem = this.sku2OrderItem(sku, num, username);
            orderItemMapper.insert(orderItem);
        }
    }

    //查询购物车列表数据
    @Override
    public Map list(String username) {
        Map map = new HashMap();

        List<OrderItem> orderItemList = orderItemMapper.selectByName(username);
        map.put("orderItemList", orderItemList);
        //商品的总数量与总价格
        Integer totalNum = 0;
        Integer totalMoney = 0;

        for (OrderItem orderItem : orderItemList) {
            totalNum += orderItem.getNum();
            totalMoney += orderItem.getMoney();
        }
        map.put("totalNum", totalNum);
        map.put("totalMoney", totalMoney);

        return map;
    }

    @Override
    public void delete(String username) {
        int orderItem = orderItemMapper.deleteByName(username);
    }

    private OrderItem sku2OrderItem(Sku sku, Integer num, String username) {
        OrderItem orderItem = new OrderItem();
        orderItem.setId(idWorker.nextId());
        orderItem.setSpuId(sku.getSpuId());
        orderItem.setSkuId(sku.getId());
        orderItem.setOrderId(idWorker.nextId());
        orderItem.setName(sku.getName());
        orderItem.setPrice(sku.getPrice());
        orderItem.setNum(num);
        orderItem.setUsername(username);
        orderItem.setMoney(orderItem.getPrice() * num);
        orderItem.setPayMoney(orderItem.getPrice() * num);
        orderItem.setImage(sku.getImage());
        orderItem.setWeight(sku.getWeight());
        orderItem.setIsReturn("0");
        return orderItem;
    }
}
